package com.lxb.store.shiro;

import com.lxb.store.entity.User;
import com.lxb.store.service.UserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

public class UserRealm extends AuthorizingRealm {

    @Autowired
    private UserService userService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        System.out.println("执行授权逻辑！");
        // 编写shiro判断逻辑，判断

        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        //System.out.println("执行认证逻辑！");
        //用户认证

        UsernamePasswordToken userToken = (UsernamePasswordToken) token;
        User user = userService.getByUsername(userToken.getUsername());
        if(user == null){
            //用户不存在，抛出UnknownAccountException
            return null;

        }
        //返回并验证密码
        return new SimpleAuthenticationInfo(user,user.getPassword(),"");
    }
}
